﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Configuration;
using System.Web.Mvc;
using System.Web.Security;
using Winter.BackEnd.Models.DataTypes;
using System.Data.Linq;

namespace Winter.BackEnd.Models.Domain
{
    public static class Repository
    {
        #region Repository
        [ThreadStatic]
        private static WinterDataContext _DataContext;

        private static aspnet_User currentUser;

        public static WinterDataContext CurrentDataContext
        {
            get
            {
                if (_DataContext == null)
                {
                    _DataContext = CreateNewDataContext();
                }

                return _DataContext;
            }
        }

        public static void UpdateCurrentDataContext()
        {
            _DataContext = CreateNewDataContext();
        }

        public static WinterDataContext CreateNewDataContext()
        {
            WinterDataContext context = new WinterDataContext(ConfigurationManager.ConnectionStrings["WinterConnectionString"].ConnectionString);

            InitLoadOptionsForContext(context);

            return context;
        }
        private static void InitLoadOptionsForContext(WinterDataContext context)
        {
            DataLoadOptions options = new DataLoadOptions();

            options.LoadWith<Person>(p => p.aspnet_User);
            options.LoadWith<aspnet_User>(u => u.aspnet_Membership);
            options.LoadWith<PersonMessage>(pm => pm.Message);
            options.LoadWith<PersonMessage>(pm => pm.Person);
            options.LoadWith<Resource>(r => r.Localizations);

            context.LoadOptions = options;
        }
        public static aspnet_User CurrentUser
        {
            get
            {
                if (Membership.GetUser() != null)
                {
                    return currentUser ?? Repository.CurrentDataContext.aspnet_Users.SingleOrDefault(u => u.UserName.ToLower() == Membership.GetUser().UserName.ToLower()); ;
                }
                //HttpContext.Current.Response.Redirect(FormsAuthentication.LoginUrl);
                return null;
            }
            set
            {
                currentUser = value;
            }
        }

        #endregion

        #region HelperMethods

        public static Message GetIncomeMessageForCurrentUserByID(int messageID)
        {
            return Repository.CurrentUser.Person.PersonMessages.FirstOrDefault(m => m.MessageID == messageID).Message;
        }

        public static Group GetGroupByID(int ID)
        {
            return CurrentDataContext.Groups.SingleOrDefault(g => g.ID == ID);

        }

        public static IPerson GetIPersonByPersonID(int ID)
        {

            return Repository.CurrentDataContext.Persons.SingleOrDefault(p => p.ID == ID).PersonInRole;
        }

        public static ParameterValue GetParameterValueByID(int ID)
        {
            return Repository.CurrentDataContext.ParameterValues.SingleOrDefault(p => p.ID == ID);
        }

        public static IList<SelectListItem> GetAllGroupForDropDown()
        {
            return (from groups in CurrentDataContext.Groups
                    select
                        new SelectListItem() { Text = groups.Name.ToString(), Value = groups.ID.ToString() }).ToList();
        }

        public static IList<SelectListItem> GetAllDayOfWeekForDropDown()
        {
            return (from days in CurrentDataContext.DayOfWeeks
                    select
                        new SelectListItem() { Text = days.DayOfWeekName.ToString(), Value = days.ID.ToString() }).ToList();
        }

        public static IList<SelectListItem> GetAllSubjectsForDropDown()
        {
            return (from sub in Repository.CurrentDataContext.Subjects
                    select
                        new SelectListItem() { Text = sub.Name.ToString(), Value = sub.ID.ToString() }).ToList();
        }

        public static IList<SelectListItem> GetAllTeachersForDropDown()
        {
            return (from teacher in Repository.CurrentDataContext.Teachers
                    select
                        new SelectListItem() { Text = teacher.Person.LastName.ToString(), Value = teacher.ID.ToString() }).ToList();
        }

        public static IEnumerable<Event> GetEventsFroGroup(int groupID)
        {
            if (Repository.CurrentDataContext.GroupSchedules.Any(g => g.GroupID == groupID))
            {
                return Repository.CurrentDataContext.GroupSchedules.First(g => g.GroupID == groupID).Schedule.Events.ToArray();
            }
            return null;
        }

        public static IEnumerable<ParameterValue> GetParameterValuesByParamenterName(string parameterName)
        {
            return Repository.CurrentDataContext.ParameterValues.Where(pv => pv.Parameter.Name.Equals(parameterName));
        }

        //public static IList<Class> GetGroupClassesForSpecificDay(int groupID, System.DayOfWeek dayOfWeek, int count)
        //{
        //    List<Class> classes = new List<Class>();

        //    for (int i = 0; i < count; i++)
        //    {
        //        if (Repository.CurrentDataContext.Classes.Any(c => c.ClassNumber == i && c.WorkDay.Schedule.GroupID == groupID && c.WorkDay.DayOfWeek.ID == (int)dayOfWeek))
        //        {
        //            classes.Add(Repository.CurrentDataContext.Classes.SingleOrDefault(c => c.ClassNumber == i && c.WorkDay.Schedule.GroupID == groupID && c.WorkDay.DayOfWeek.ID == (int)dayOfWeek));
        //        }
        //        classes.Add(new Class() { ID = 0, Subject = new Subject() { Name = " " } });
        //    }
        //    return classes;
        //}
        //public static IList<ClassDTO> GetGroupClassesForSpecificDayInJSON(int groupID, System.DayOfWeek dayOfWeek, int count)
        //{
        //    List<ClassDTO> classes = new List<ClassDTO>();

        //    for (int i = 0; i < count; i++)
        //    {
        //        if (Repository.CurrentDataContext.Classes.Any(c => c.ClassNumber == i && c.WorkDay.Schedule.GroupID == groupID && c.WorkDay.DayOfWeek.ID == (int)dayOfWeek))
        //        {
        //            classes.Add(new ClassDTO()
        //            {
        //                ID = Repository.CurrentDataContext.Classes.SingleOrDefault(c => c.ClassNumber ==
        //                       i && c.WorkDay.Schedule.GroupID == groupID && c.WorkDay.DayOfWeek.ID == (int)dayOfWeek).ID,
        //                Subject =
        //                   Repository.CurrentDataContext.Classes.SingleOrDefault(c => c.ClassNumber ==
        //                       i && c.WorkDay.Schedule.GroupID == groupID && c.WorkDay.DayOfWeek.ID == (int)dayOfWeek).Subject.Name
        //            });
        //        }
        //        classes.Add(new ClassDTO() { ID = 0, Subject = " " });
        //    }
        //    return classes;
        //}
        #endregion
    }
}
